package com.hqyj.javaSpringBoot.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.account.dao.ResourceDao;
import com.hqyj.javaSpringBoot.modules.account.dao.RoleResourceDao;
import com.hqyj.javaSpringBoot.modules.account.entity.Resource;
import com.hqyj.javaSpringBoot.modules.account.entity.Role;
import com.hqyj.javaSpringBoot.modules.account.entity.RoleResource;
import com.hqyj.javaSpringBoot.modules.account.service.ResourceService;
import com.hqyj.javaSpringBoot.modules.common.entiy.ResultEntity;
import com.hqyj.javaSpringBoot.modules.common.entiy.SearchBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Id;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceDao resourceDao;
    @Autowired
    private RoleResourceDao roleResourceDao;

    @Override
    @Transactional
    public ResultEntity<Resource> insertResource(Resource resource) {
        List<Resource> resources = Optional.ofNullable(resourceDao.getResourceByPerssion(
                resource.getPerssion())).orElse(Collections.emptyList());
        if (resources.isEmpty()){
            resource.setCreateDate(LocalDateTime.now());
            resourceDao.insertResource(resource);
        }
        if (resource.getRoles()!=null ){
            resource.getRoles().stream().forEach(item->{
                roleResourceDao.insertRoleResource(new RoleResource(item.getId(),resource.getId()));
            });
        }
        return new ResultEntity<Resource>(ResultEntity.
                ResultStatus.SUCCESS.code,"insert success",resource);
    }

    @Override
    @Transactional
    public ResultEntity<Resource> deleteResourceByReourceId(int id) {
        resourceDao.deleteResourceByReourceId(id);
        roleResourceDao.deleteRoleResourceByResourceId(id);
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.code,
                "delete success");
    }

    @Override
    @Transactional
    public ResultEntity<Resource> updateResourceByReourceId(Resource resource) {
        resourceDao.updateResourceByReourceId(resource);
        if (resource.getRoles()!=null){
            roleResourceDao.deleteRoleResourceByResourceId(resource.getId());
            resource.getRoles().stream().forEach(item->{
                roleResourceDao.insertRoleResource(new RoleResource(item.getId(),resource.getId()));
            });
        }
        return new ResultEntity<Resource>(ResultEntity.ResultStatus.SUCCESS.code,
                "update success",resource);
    }

    @Override
    public Resource getResourceByResourceId(int id) {
        return  resourceDao.getResourceByResourceId(id);
    }

   @Override
    public PageInfo<Resource> getResources(SearchBean searchBean) {
        searchBean.initSerchBean();
        PageHelper.startPage(searchBean.getCurrentPage(),searchBean.getPageSize());
        return new PageInfo<Resource>(Optional.ofNullable(
                resourceDao.getResources(searchBean)).
                orElse(Collections.emptyList()));
    }

    @Override
    public List<Resource> getResourceByRoleID(int roleId) {
        return resourceDao.getResourceByRoleID(roleId);
    }
}
